int fd = ASharedMemory_create("memory", 128);
// By default it has PROT_READ | PROT_WRITE | PROT_EXEC. size_t memSize = ASharedMemory_getSize(fd); char *buffer = (char *) mmap(NULL, memSize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
strcpy(buffer, "This is an example."); // trivially initialize content
// limit access to read only ASharedMemory_setProt(fd, PROT_READ);
// share fd with another process here and the other process can only map with PROT_READ.
Available since API level 26.
\param fd file descriptor of the shared memory region. \param prot any bitwise-or'ed combination of PROT_READ, PROT_WRITE, PROT_EXEC denoting updated access. Note access can only be removed, but not added back. \return 0 for success, error code on failure.
Restrict access of shared memory region.
This function restricts access of a shared memory region. Access can only be removed. The effect applies globally to all file descriptors in all processes across the system that refer to this shared memory region. Existing memory mapped regions are not affected.
It is a common use case to create a shared memory region, map it read/write locally to intialize content, and then send the shared memory to another process with read only access. Code example as below (error handling omited).